package cc.wfu.types.weixin;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class LantuPayUtil {
    public static final String mchId = "1673361595";
    public static final String notifyUrl = "http://3901d67d.r10.cpolar.top/api/v1/sale/ltzf/pay_notify";
    public static final String key = "157f45dbe6bb9d903841713f2caabf0f";
    public static void main(String[] args){
        //商户订单号，只能是数字、大小写字母_-且在同一个商户号下唯一。
        String out_trade_no = "2023" + System.currentTimeMillis();
        String total_fee = "0.01";        //支付金额
        //商品描述
        String body = "商品描述参数";
        Long stime = System.currentTimeMillis()/1000;
        //当前时间戳
        String timestamp = String.valueOf(stime);
        Map<String, Object> map = new HashMap<>();
        map.put("mch_id", mchId);
        map.put("out_trade_no", out_trade_no);
        map.put("total_fee", total_fee);
        map.put("body", body);
        map.put("notify_url", notifyUrl);
        map.put("timestamp", timestamp);
        String sign = PaySignUtil.createSign(map, key);
        map.put("sign", sign);
        //微信扫码支付接口地址
        String url = "https://api.ltzf.cn/api/wxpay/native";
        String result = HttpRequest.post(url).form(map).execute().body();
        System.out.println(result);
        String msg = "";
        if (StrUtil.isBlank(result)) {
            msg = "API接口返回为空，请联系客服";
        }else{
            try{
                JSONObject jsonObject = (JSONObject)JSONObject.parse(result);
                Integer code = jsonObject.getInteger("code");
                if(code==0){
                    String qrcode = jsonObject.getJSONObject("data").getString("QRcode_url");
                    msg = "请使用微信扫码下方二维码:" + qrcode;
                }else{
                    String msgstr = jsonObject.getString("msg");
                    msg = "返回失败：" + msgstr;
                }
            } catch (Exception e) {
                msg = e.getMessage();
            }
        }
        log.info(msg);
    }
}